package com.ruoyi.smart.config;

import com.ruoyi.common.utils.sign.Md5Utils;
import org.ektorp.CouchDbConnector;
import org.ektorp.CouchDbInstance;
import org.ektorp.http.HttpClient;
import org.ektorp.http.StdHttpClient;
import org.ektorp.impl.StdCouchDbConnector;
import org.ektorp.impl.StdCouchDbInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CouchDBConfig {

    @Autowired
    CouchDBInfo couchDBInfo;

    private static final Logger log = LoggerFactory.getLogger(CouchDBConfig.class);

    public CouchDbConnector couchDbConnector(String databaseName) throws Exception {
        HttpClient httpClient = new StdHttpClient.Builder().url(couchDBInfo.getHost() + ":"+ couchDBInfo.getPort())
                .username(couchDBInfo.getUsername()).connectionTimeout(10000).socketTimeout(1000000)
                .password(couchDBInfo.getPassword()).build();
        CouchDbInstance couchDbInstance = new StdCouchDbInstance(httpClient);
        CouchDbConnector couchDbConnector = new StdCouchDbConnector(databaseName, couchDbInstance);
        couchDbConnector.createDatabaseIfNotExists();
        log.info("CouchDB数据库:"+databaseName+"连接成功");
        return couchDbConnector;
    }



}
